package com.example.lovedogs.shop;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.Attributes.Name;

import com.example.lovedogs.R;
import com.example.lovedogs.domain.goods;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class GoodsListActivity extends Activity {
	private int[] imageId = { R.drawable.image01, R.drawable.image02 };
	private String[] names = { "小狗开胃宝", "胃健康" };
	private String[] nowprice = { "现价$30", "现价$30.4" };
	private String[] oldprice = { "原价$33", "原价$34.5" };
	private String[] Number = { "已售：344", "已售：454" };
	private ListView list;
	private String url = "http://10.0.2.2:8080/LoveDogs04/JsonGoodsInfoDiseaseServlet";
	private MyAdapter adapter;
	private SimpleAdapter adapter1; 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_goods_list);
		Intent intent = new Intent();
		String dateString = intent.getStringExtra("no");
		initGridView(dateString);
	}
	private void initGridView(String data) {
		// 1
		RequestParams params = new RequestParams();
		params.addBodyParameter("key", data);
		// 2
		HttpUtils http = new HttpUtils();
		// 3
		http.send(HttpMethod.POST, url, params, new RequestCallBack<String>() {
			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				//创建一个List集合，List集合的元素是Map
				List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
				Map<String, Object> lisItem = new HashMap<String, Object>();
				for (int i = 0; i < FOCUSED_STATE_SET.length; i++) {
					lisItem.put("header", imageId[i]);
					lisItem.put("goodsname", names[i]);
					lisItem.put("nprice", nowprice[i]);
					lisItem.put("oprice", oldprice[i]);
					lisItem.put("number", Number[i]);
					listItems.add(lisItem);
				}
				if (adapter == null) {
					adapter1 = new SimpleAdapter(GoodsListActivity.this, listItems,R.layout.listitem_goodslist_item, 
							new String[] { "header","goodsname", "nprice", "oprice", "number" }, 
							new int[] {R.id.header, R.id.goodsname, R.id.nprice, R.id.oprice,R.id.number });
			list = (ListView) findViewById(R.id.listView2);
			list.setAdapter(adapter1);
				//调用setOnItemClickListener()方法实现单击选项！！！
			list.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					if (position==0) {
						Intent intent = new Intent(GoodsListActivity.this, BabyActivity.class);
//						goods sdemo = (goods)adapter.getItem(position);
//						int no = sdemo.getNo();
//						SpannableStringBuilder sp=adapter.getSp();
//						int GoodsClass = sdemo.getClassno();
//						intent.putExtra("no", no);
//						intent.putExtra("sp", sp);
						startActivity(intent);
					}}
			});
			}
				/*
				 * Gson g=new Gson(); Type type=new
				 * TypeToken<List<Goodskinds>>(){}.getType(); List<Goodskinds>
				 * ldata=g.fromJson(responseInfo.result, type);
				 */
			}
			@Override
			public void onFailure(HttpException error, String msg) {
				Toast.makeText(GoodsListActivity.this, "error", Toast.LENGTH_LONG)
						.show();
			}
		});
	}
	// 自定义适配器
	class MyAdapter extends BaseAdapter {
		private List<Map<String, goods>> data;
		private LayoutInflater inflater;
		private SpannableStringBuilder sp;
		
		public MyAdapter(List<Map<String, goods>> data) {
			this.data = data;
			inflater = LayoutInflater.from(GoodsListActivity.this);
		}

		public List<Map<String, goods>> getData() {
			return data;
		}

		public void setData(List<Map<String, goods>> data) {
			this.data = data;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return data.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return data.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
		public SpannableStringBuilder getSp() {
			return sp;
		}

		public void setSp(SpannableStringBuilder sp) {
			this.sp = sp;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			goods info = (goods) data.get(position);
			if (convertView == null) {
				convertView = inflater.inflate(
						R.layout.listitem_goodslist_item, null);
			}
			ImageView imageView = (ImageView) findViewById(R.id.header);
//需要改变图片
			imageView.setImageResource(R.drawable.image01);
			TextView textView_goodsname = (TextView) findViewById(R.id.goodsname);
			textView_goodsname.setText(info.getName());
			TextView textView_number = (TextView)findViewById(R.id.number);
			textView_number.setText(info.getSalesnumber());
			TextView textView_nprice = (TextView)findViewById(R.id.nprice);
			textView_nprice.setText(info.getPrice());
			TextView textView_oprice = (TextView)findViewById(R.id.oprice);
			textView_oprice.setText(info.getMarketprice());
			return convertView;
		} 
	}
	// 自定义点击事件实现类
//	private class MyOnItemClickListener implements OnItemClickListener {
//		@Override
//		public void onItemClick(AdapterView<?> parent, View view, int position,
//				long id) {
//			goods sdemo = (goods)adapter.getItem(position);
//			int no = sdemo.getNo();
//			SpannableStringBuilder sp=adapter.getSp();
//			int GoodsClass = sdemo.getClassno();
//			if (position==0) {
//				Intent intent = new Intent(GoodsListActivity.this, GoodsListDetailActivity.class);
//				intent.putExtra("no", no);
//				intent.putExtra("sp", sp);
//				startActivity(intent);
//			}else {
//             System.out.println("有错！！！");
//			}
//		}

//	}


}
